package com.neusoft.controller;

import com.neusoft.entity.Demand;
import com.neusoft.entity.common.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;

@Controller
@RequestMapping("demandConsumer")
public class DemandConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    //    private String url_prefix = "http://localhost:8080";//"http://localhost:8080"
    private String url_prefix = "http://localhost:8001";

    @PostMapping("save")
    @ResponseBody
    public void insert(Demand bean){
        String url = url_prefix + "/demand/save";

        restTemplate.put(url,bean);
    }

    @RequestMapping("queryAll")
    @ResponseBody
    public Object queryAll(Integer page, Integer limit, Demand bean){

        String url = url_prefix + "/demand/queryAll/"+page+"/"+limit;
        System.out.println(url);

        Object result=restTemplate.postForObject(url,bean,CommonResult.class);
        return  result;

    }
    @RequestMapping("queryById/{id}")
    @ResponseBody
    public Object queryById(@PathVariable Integer id) {
        String uri = url_prefix + "/demand/queryById/" + id;
        return restTemplate.getForObject(uri, Demand .class);
    }

    @PostMapping("deleteById")
    @ResponseBody
    public ResponseEntity<Boolean> deleteById(Integer[] ids){
        for(int i=0;i<ids.length;i++) {
            System.out.println("ids值为：" + ids[i]);
        }
        StringBuffer sb = new StringBuffer();
        System.out.println(Arrays.toString(ids));
        sb.append(Arrays.toString(ids));
        sb.deleteCharAt(sb.indexOf("["));
        sb.deleteCharAt(sb.lastIndexOf("]"));
        System.out.println(sb.toString());
        String url = url_prefix+"/demand/deleteById/" + sb.toString();
        return restTemplate.postForEntity(url,sb.toString(),Boolean.class);
    }

    @PostMapping("deleteById1/{id}")
    @ResponseBody
    public ResponseEntity<Boolean> deleteById1(@PathVariable Integer id){
        String url = url_prefix+"/demand/delete1/" + id;
        return restTemplate.postForEntity(url,id,Boolean.class);
    }

    @RequestMapping("toList")
    public String toList() {
        return "user/demand_list";
    }

    @RequestMapping("toEdit")
    public String toEdit(Integer id, Model model){
        Demand  bean = (Demand ) queryById(id);
        model.addAttribute("bean",bean);
        return "user/demand_add";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model){
        model.addAttribute("bean",new Demand ());
        return "user/demand_add";
    }
    @RequestMapping("toRoomList")
    public String toRoomList(Integer id,Model model) {


        System.out.println(id);

        Demand  bean = (Demand ) queryById(id);
        model.addAttribute("bean",bean);

        return "user/searchroom_list";
    }

}
